package com.tanggh.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.tanggh.util.CookieManager;
import com.tanggh.util.Tools;

public class LoginInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 1L;
	private String url;
	

	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}


	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ctx = invocation.getInvocationContext();
		HttpServletRequest request= (HttpServletRequest) ctx.get(StrutsStatics.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ctx.get(StrutsStatics.HTTP_RESPONSE);
		String userId = CookieManager.getInstance().getValue(request, CookieManager.USER_ID);
		if(null == userId) {
			System.out.println("not login");
			String url = "/login?url="+Tools.getLoginURL(request);
			setUrl(url);
			System.out.println("url:"+url);
			Tools.sendRedirect(response, url);
			return null;
		}
		else{
			System.out.println("login..");
		}
		return invocation.invoke();
	}
}
